﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;

namespace DL
{
    class Interpret
    {

        Hashtable symbolsTable = new Hashtable();
        public Graphics graphics;
        Pen pen;

        public Interpret()
        {
            pen = new Pen(Color.Black, 1);
        }

        public void ParseInputFile(StreamReader file)
        {
            while (!file.EndOfStream)
            {
                Parse(file.ReadLine());
            }
        }

        public void Parse(String line)
        {
            string[] tokens = line.Split(new char[] { ' ' });

            switch (tokens[0])
            {
                case "point": { ParsePoint(tokens[1], tokens[2], tokens[3]); break; }
                case "drawline": { DrawLine(tokens[1], tokens[2]); break; }
                default: { /*TODO: Error: Unknown command*/ break; }
            }

        }

        public void ParseName(String IdName)
        {
            Regex isId = new Regex("");
        }

        public void ParsePoint(String name, String argX, String argY)
        {
            int x = 0, y = 0;
            try
            {
                ParseName(name);
                x = Int32.Parse(argX);
                y = Int32.Parse(argY);
            }
            catch
            {
            }

            Point p = new Point();
            p.X = x;
            p.Y = y;

            if (!symbolsTable.ContainsKey(name))
                symbolsTable.Add(name, p);
            else setError();
        }


        public void setError()
        {

        }
        public void DrawLine(String token1, String token2)
        {
            if (symbolsTable.ContainsKey(token1) && symbolsTable.ContainsKey(token2))
            {
                Point point1 = (Point)symbolsTable[token1];
                Point point2 = (Point)symbolsTable[token2];

                graphics.DrawLine(pen, point1, point2);
            }
        }

        public void ParseBezier()
        {

            Point p1 = new Point(10, 10);
            Point p2 = new Point(80, 90);
            Point p3 = new Point(130, 160);
            Point p4 = new Point(150, 100);
            Point p5 = new Point(80, 110);
            Point p6 = new Point(130, 160);
            Point p7 = new Point(150, 120);
            Point p8 = new Point(10, 30);

            BezierCurve curve = new BezierCurve(p1, p8, p3, p4, p6);
            curve.Draw(pen, graphics);
        }
    }
}
